<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\cainiao\OrderModel;
use App\Models\cainiao\BigBagModel;
use App\Models\CNLogModel;
use App\Models\CountryModel;
use App\Models\CustomerModel;
use Illuminate\Http\Request;


class Tm extends Controller
{

    public function __construct(Request $request)
    {
        $action = $request->route()->getActionMethod();
        if($action == 'asn' || $action == 'notify' || $action == 'update'){
            $data = $request->all();

            $log = New CNLogModel();
            $log->data = json_encode($data);
            $log->ip = $request->ip();
            $log->type = $data['msg_type'];
            $log->platform = 'tm';
            $log->save();
        }
    }


    public function asn(Request $request)
    {
        $data = $request->all();
        $logistics_interface = json_decode($data['logistics_interface'], true);

        $return = [
            'success' => true
        ];

//        $find = AsnModel::where('logisticsOrderCode', $logistics_interface['logisticsOrderCode'])->first();
//        if($find){
//            return response()->json($return);
//        }

        $customer_id = CustomerModel::where('is_big', 1)->where('name', '天猫')->first()->id ?? 0;

        $price = $logistics_interface['parcel']['price'] ?? 0;
        $weight = $logistics_interface['parcel']['weight'] ?? 0;
        $parcelType = $logistics_interface['parcel']['parcelType'] ?? '';
        $goods_attr = OrderModel::GOODS_ATTR[$parcelType] ?? 1;
        $customs = isset($logistics_interface['customs']) ? $logistics_interface['customs'] : '';
        if(is_array($customs)){
            $customsCode = $customs['customsCode'] ?? '';
        }else{
            $customsCode = '';
        }

        $asn = new OrderModel();
        $asn->platform = 'tm';
        $asn->customer_id = $customer_id;
        $asn->msg_type = $data['msg_type'];
        $asn->partner_code = $data['partner_code'];
        $asn->from_code = $data['from_code'];
        $asn->logisticsOrderCode = $logistics_interface['logisticsOrderCode'];
        $asn->trackingNumber = $logistics_interface['trackingNumber'] ?? '';
        $asn->consoCPResCode = $logistics_interface['consoCPResCode'] ?? '';
        $asn->carrierCode = $logistics_interface['carrierCode'] ?? '';
        $asn->customsCode = $customsCode;
        $asn->waybillUrl = $logistics_interface['waybillUrl'] ?? '';
        $asn->waybillNumber = $logistics_interface['waybillNumber'] ?? '';
        $asn->bigBagID = $logistics_interface['parcel']['bigBagID'] ?? '';
        $asn->sender = isset($logistics_interface['sender']) ? json_encode($logistics_interface['sender']) : '';
        $asn->buyer = isset($logistics_interface['buyer']) ? json_encode($logistics_interface['buyer']) : '';
        $asn->receiver = isset($logistics_interface['receiver']) ? json_encode($logistics_interface['receiver']) : '';
        $asn->popStation = isset($logistics_interface['popStation']) ? json_encode($logistics_interface['popStation']) : '';
        $asn->pickup = isset($logistics_interface['pickup']) ? json_encode($logistics_interface['pickup']) : '';
        $asn->parcel = isset($logistics_interface['parcel']) ? json_encode($logistics_interface['parcel']) : '';
        $asn->customs = isset($logistics_interface['customs']) ? json_encode($logistics_interface['customs']) : '';
        $asn->returnParcel = isset($logistics_interface['returnParcel']) ? json_encode($logistics_interface['returnParcel']) : '';
        $asn->trade = isset($logistics_interface['trade']) ? json_encode($logistics_interface['trade']) : '';
        $asn->price = $price;
        $asn->parcelType = $parcelType;
        $asn->goods_attr = $goods_attr;
        $asn->weight = $weight;
        $asn->save();

        $des_country_code = $logistics_interface['receiver']['address']['country'] ?? '';

        $des_find = CountryModel::where('erzi_code', $des_country_code)->orWhere('name', $des_country_code)->orWhere('name_en', $des_country_code)->first();
        if($des_find){
            $notify = BigBagModel::where('bigBagID', $logistics_interface['parcel']['bigBagID'])->first();
            if($notify){
                $notify->des_country_id = $des_find->id;
                $notify->save();
            }
            $asn->des_country_id = $des_find->id;
            $asn->save();
        }

        return response()->json($return);
    }

    public function notify(Request $request)
    {
        $data = $request->all();
        $logistics_interface = json_decode($data['logistics_interface'], true);

        $return = [
            'success' => true
        ];

        $customer_id = CustomerModel::where('is_big', 1)->where('name', '天猫')->first()->id ?? 0;

        $find = BigBagModel::where('bigBagID', $logistics_interface['bigBagID'])->first();
        if($find){
            $notify = $find;
        }else{
            $notify = new BigBagModel();
        }

        $notify->platform = 'tm';
        $notify->customer_id = $customer_id;
        $notify->msg_type = $data['msg_type'];
        $notify->partner_code = $data['partner_code'];
        $notify->from_code = $data['from_code'];
        $notify->preCPResCode = $logistics_interface['preCPResCode'] ?? '';
        $notify->labelWeight = $logistics_interface['labelWeight'] ?? 0;
        $notify->currentCPResCode = $logistics_interface['currentCPResCode'];
        $notify->laneCode = $logistics_interface['laneCode'];
        $notify->parcelList = json_encode($logistics_interface['parcelList']);
        $notify->standardWeight = $logistics_interface['standardWeight'] ?? 0;
        $notify->sortCode = $logistics_interface['sortCode'];
        $notify->packingWeight = $logistics_interface['packingWeight'] ?? 0;
        $notify->grossWeight = $logistics_interface['grossWeight'] ?? 0;
        $notify->netWeight = $logistics_interface['netWeight'] ?? 0;
        $notify->bigBagID = $logistics_interface['bigBagID'] ?? '';
        $notify->parcelQty = $logistics_interface['parcelQty'] ?? 0;
        $notify->weightUnit = $logistics_interface['weightUnit'] ?? '';
        $notify->bigBagType = $logistics_interface['bigBagType'] ?? '';
        $notify->features = $logistics_interface['features'] ?? '';
        $notify->save();

        return response()->json($return);
    }

    public function update(Request $request)
    {
        $data = $request->all();

        $logistics_interface = json_decode($data['logistics_interface'], true);

        $return = [
            'success' => true
        ];

        $bigBagID = $logistics_interface['bigBagID'];

        $find = BigBagModel::where('bigBagID', $bigBagID)->first();

        if($find){
            $logisticsOrderCode = $logistics_interface['parcelList']['Parcel']['logisticsOrderCode'];

            $asn_find = OrderModel::where('logisticsOrderCode', $logisticsOrderCode)->first();

            if($asn_find && $logistics_interface['updateType'] == 'DELETE'){
                $asn_find->delete();
            }
        }

        return response()->json($return);
    }



    public function test(Request $request)
    {
        $id = $request->input('id');

        $data = CNLogModel::find($id);

        $return = json_decode($data->data, true);

        $return['logistics_interface'] = json_decode($return['logistics_interface'], true);

//        $all = LazadaModel::get();
//
//        $return[] = $all->map(function ($item) {
//            $data = json_decode($item->data, true);
//
//            $logistics_interface = json_decode($data['logistics_interface'], true);
//            return $logistics_interface['logisticsOrderCode'] ?? '';
//        });



        return $this->success($return);
    }

}
